const app = getApp()
Page({


  data: {
    cloudImgList:[],
    inputValue:'',
    localtion:'请选择位置',
    addresssInfo:{}
  },

 chooseVideo(e){
    var that = this;
    wx.chooseVideo({
      sourceType: ['album','camera'],
      maxDuration: 60,
      compressed: false,
      camera: 'back',
      async success(res) {
        wx.showLoading({
          title: '上传中',
          mask:true
        })
        var r =  await that.upload(res.tempFilePath);
        var data = JSON.parse(r.data);
        console.log(data);
        that.setData({
          videoUrl: data.data.url
        })
        wx.hideLoading();
      }
    })
  },

  chooseAddress(){
    var that = this;
    wx.chooseLocation({
      success(res){
        console.log(res)
        that.setData({
          localtion:res.address + res.name,
          addresssInfo:res
        })

        if(res.address==""){
          that.setData({
            localtion:"请选择位置"
          })
        }
      },
      fail(res){
        debugger
        console.log(res)
      }
    })
  },

  onLoad: function (options) {

    console.log(app.globalData.userInfo)

  },

  getValue(e){
    console.log(e.detail.value)
    this.setData({
      inputValue: e.detail.value
    })
  },

  chooseImage(){
    var that = this;
    wx.chooseImage({
      count: 9 - that.data.cloudImgList.length,
      sizeType: ['original','compressed'],
      sourceType: ['album','camera'],
      success(res){

        console.log(res)
        console.log(res.tempFilePaths)
        //上传图片
        that.data.tempImgList = res.tempFilePaths
        that.uploadImages()
      }
    })

  },

  async uploadImages(){
    var that = this;
    for(var i = 0 ; i< this.data.tempImgList.length; i++){
      var res =await that.upload(this.data.tempImgList[i]);
      var url = JSON.parse(res.data).data.url;
      that.data.cloudImgList.push(url)
      that.setData({
        cloudImgList:that.data.cloudImgList
      })

    }

  },
  deleteImg(e){
    console.log(e.currentTarget.dataset.index)
    this.data.cloudImgList.splice(e.currentTarget.dataset.index,1)
    this.setData({
      cloudImgList: this.data.cloudImgList
    })
  },

  submitData(){
    var that = this;
    wx.showLoading({
      title: '发布中',
      mask:'true'
    })
    if(!this.data.inputValue && !that.data.videoUrl&& this.data.cloudImgList.length ==0){
      wx.showToast({
        icon:'none',
        title: '发布为空',
      })
      return
    }
    wx.request({
      url: app.globalData.baseUrl + 'api/actions/v1/wx/saveOrUpdate',
      method:"POST",
      data:{
        cOpenId: app.globalData.openid,
        cNickName: app.globalData.userInfo.cNickName,
        cFaceImg: app.globalData.userInfo.cIcon,
        cText: this.data.inputValue,
        images: this.data.cloudImgList,
        dtCreate: Date.now(),
        prizeList: [],
        commentList: [],
        cLatitude: that.data.addresssInfo.latitude,
        cLongitude: that.data.addresssInfo.longitude,
        cAddress: that.data.addresssInfo.address + that.data.addresssInfo.name,
        cVideo: that.data.videoUrl
      },
      success(res){
        wx.navigateBack({
          success(res){
            wx.hideLoading({
              success: (res) => {},
            })
            wx.showToast({
              title: '发布成功！',
            })
          }
        })
      }
    })
  },
  
    // 上传图片逻辑
  async upload(filePath) {
      return await new Promise((resolve, reject) => {
        wx.uploadFile({
          // 上传至服务器地址
          url: app.globalData.baseUrl + 'api/file/imgUpload',
          // 文件路径
          filePath: filePath,
          // 名称
          name: 'file',
          success(res) {
            resolve(res);
          },
          fail(res) {
            reject(res);
          }
        })
      })
    }
})